﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the API Concepts Framework
 * http://apiconceptsframework.codeplex.com
 *
 * Copyright (C) 2014 eva Kühn, Thomas Scheller, Vienna University of Technology
 * http://www.complang.tuwien.ac.at/eva
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Reflection;
using ApiConceptsFramework.Contract;

namespace ApiConceptsFramework.Concepts.Properties
{
	public class UsageOfReturnParameter : IMeasurableProperty
	{
		internal const int ReturnParameterComplexity = 8; //return parameter is rated separately

		public float CalculateComplexity(MeasurableTarget target, IDE ide = IDE.VisualStudioOrSimilar)
		{
			if (!(target.Member is MethodInfo) && !(target.Member is ConstructorInfo))
				throw new ArgumentException("target.Member must be a MethodInfo or ConstructorInfo!", "target");

			var complexity = 0;
			bool hasUsedReturnParameter = HasUsedReturnParameter(target);
			if (HasUsedReturnParameter(target))
			{
				complexity += ReturnParameterComplexity;
				CalculationInfo = "return parameter is used";
			}
			else
				CalculationInfo = "return parameter is not used";
			return complexity;
		}

		private static bool HasUsedReturnParameter(MeasurableTarget target)
		{
			return (target.Member is MethodInfo && ((MethodInfo)target.Member).ReturnType != typeof(void)) && target.ReturnedValueIsUsed;
		}

		public IEnumerable<string> GetSuggestions(MeasurableTarget target, IDE ide = IDE.VisualStudioOrSimilar)
		{
			return new string[] {};
		}

		public string CalculationInfo { get; private set; }
	}
}